<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Booking extends CI_Controller {

	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('branch_model');
        $this->load->model('movie_model');
        $this->load->model('schedule_model');
        $this->load->model('cinema_model');
        $this->load->model('booking_model');
        $this->load->model('content_model');
    } 
	public function index()
	{
	    $layout = array();
	    $layout['branchs'] = $this->branch_model->get_all();
	    $layout['contents']= $this->content_model->get_all(5);
	    $layout = $this->load->view('booking/index',$layout,TRUE);
		$this->load->view('layout/template' , array("content" => $layout) );
	}
	public function book($schedule_id)
	{
	    if($this->session->userdata('id') == ''){
	        redirect('booking');
	    }
	    $this->form_validation->set_rules('schedule_id','qty','required');
	    $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    
	    $layout = array();
	    $schedule = $this->schedule_model->get_by_id($schedule_id);
	    $cinema = $this->cinema_model->get_by_id($schedule->cinema_id );
	    $branch = $this->branch_model->get_by_id($cinema->branch_id );
	    $layout['schedule'] =$schedule ;
	    $layout['movie'] = $this->movie_model->get_by_id($schedule->movie_id );
	    $layout['cinema'] = $cinema;
	    $layout['branch'] = $branch;
	    
	    
	    if($this->form_validation->run()){
	        $schedule_id = $this->input->post('schedule_id', TRUE);
            $qty = $this->input->post('qty', TRUE);
            for($i =0; $i <$qty ;$i++){
                $booking_id = $this->booking_model->create($schedule_id,$this->session->userdata('id') );
            }
            $layout['qty'] = $qty;
            $layout['subtotal'] = $qty*140;
            $layout = $this->load->view('booking/complete',$layout,TRUE);
	    }else{
	        $layout = $this->load->view('booking/book',$layout,TRUE);
	    }
	    
	    
		$this->load->view('layout/template' , array("content" => $layout) );
	}
	
	public function get_movie_by_branch($branch_id)
    {
	    $layout['movies'] = $this->movie_model->get_by_branch_id($branch_id);
        $layout['branch'] = $this->branch_model->get_by_id($branch_id);
        
	    $this->load->view('booking/get_movie_by_branch',$layout);
    }
    
	public function get_schedule_by_movie($movie_id,$branch_id)
    {
	    $layout['schedules'] = $this->schedule_model->get_by_movie_id_and_branch_id($movie_id,$branch_id);
	    $layout['movie'] = $this->movie_model->get_by_id($movie_id);
	    $layout['branch'] = $this->branch_model->get_by_id($branch_id);
	    $this->load->view('booking/get_schedule_by_movie',$layout);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */